﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SSN.DotNet;
using SSN.BusinessFramework;
using SSN.WebFramework;
using SSN.BusinessFramework.Accounts;

namespace SSN.WebFramework.Controls
{
    /// <summary>
    /// Note: Requires javascript
    /// </summary>
    public class AccountTextBox : System.Web.UI.WebControls.TextBox, IBindable
    {
        /// <summary>
        /// Gets or sets whether the control will have the default focus when the page loads.
        /// </summary>
        public bool HasFocus { get; set; }

        /// <summary>
        /// Indicates what type of account number will be used (tradedesk, snnumber or either)
        /// </summary>
        public AccountTextType AccountTextType { get; set; }

        public AccountTextBox()
        {
            Attributes["size"] = "12";
            AccountTextType = WebFramework.Controls.AccountTextType.Either;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            CssClass = CssClass + " account";

            Attributes["type"] = AccountTextType == WebFramework.Controls.AccountTextType.Either ? "either" : (AccountTextType == WebFramework.Controls.AccountTextType.SNNumber ? "snnumber" : "tradedesk");

            base.Render(writer);
        }

        public string BindProperty { get; set; }

        public void BindFromObject(object obj)
        {
            PropertyInfo info=obj.GetType().GetProperty(BindProperty);
		  if(info!=null) {
			  object v = info.GetValue(obj,null);
			  if(v != null) {
				  this.Text = v.ToString().Trim();
			  }
		  }
        }

        public string BindTranslator { get; set; }

	   public void BindToObject(object obj) {
		   PropertyInfo info=obj.GetType().GetProperty(BindProperty);
		   if(info!=null) {
			   info.SetValue(obj,Convert.ChangeType(this.Text,info.PropertyType),null);
		   }
	   }
    }
}
